package com.lrkj.payfee.service.impl;

import com.lrkj.common.utils.DateUtils;
import com.lrkj.payfee.domain.ReceiptDetail;
import com.lrkj.payfee.mapper.ReceiptDetailMapper;
import com.lrkj.payfee.service.IReceiptDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 收款单明细Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-08-06
 */
@Service
public class ReceiptDetailServiceImpl implements IReceiptDetailService {
    @Autowired
    private ReceiptDetailMapper receiptDetailMapper;

    /**
     * 查询收款单明细
     *
     * @param receiptDetailId 收款单明细ID
     * @return 收款单明细
     */
    @Override
    public ReceiptDetail selectReceiptDetailById(String receiptDetailId) {
        return receiptDetailMapper.selectReceiptDetailById(receiptDetailId);
    }

    /**
     * 查询收款单明细列表
     *
     * @param receiptDetail 收款单明细
     * @return 收款单明细
     */
    @Override
    public List<ReceiptDetail> selectReceiptDetailList(ReceiptDetail receiptDetail) {
        return receiptDetailMapper.selectReceiptDetailList(receiptDetail);
    }

    /**
     * 新增收款单明细
     *
     * @param receiptDetail 收款单明细
     * @return 结果
     */
    @Override
    public int insertReceiptDetail(ReceiptDetail receiptDetail) {
        receiptDetail.setCreateTime(DateUtils.getNowDate());
        return receiptDetailMapper.insertReceiptDetail(receiptDetail);
    }

    /**
     * 修改收款单明细
     *
     * @param receiptDetail 收款单明细
     * @return 结果
     */
    @Override
    public int updateReceiptDetail(ReceiptDetail receiptDetail) {
        receiptDetail.setUpdateTime(DateUtils.getNowDate());
        return receiptDetailMapper.updateReceiptDetail(receiptDetail);
    }

    /**
     * 批量删除收款单明细
     *
     * @param receiptDetailIds 需要删除的收款单明细ID
     * @return 结果
     */
    @Override
    public int deleteReceiptDetailByIds(String[] receiptDetailIds) {
        return receiptDetailMapper.deleteReceiptDetailByIds(receiptDetailIds);
    }

    /**
     * 删除收款单明细信息
     *
     * @param receiptDetailId 收款单明细ID
     * @return 结果
     */
    @Override
    public int deleteReceiptDetailById(String receiptDetailId) {
        return receiptDetailMapper.deleteReceiptDetailById(receiptDetailId);
    }

    @Override
    public int doSaveList(List<ReceiptDetail> batchBean) {
        List<ReceiptDetail> saveBeans = new ArrayList<>();
        if (batchBean.size() > 500) {
            for (int i = 0; i < batchBean.size(); i++) {
                if ((i + 1) % 500 == 0) {
                    saveBeans.add(batchBean.get(i));
                    receiptDetailMapper.doSaveList(saveBeans);
                    saveBeans.clear();
                } else {
                    saveBeans.add(batchBean.get(i));
                }
            }
            if (null != saveBeans && saveBeans.size() > 0) {
                receiptDetailMapper.doSaveList(saveBeans);
            }
        } else {
            if (batchBean.size() > 0) {
                receiptDetailMapper.doSaveList(batchBean);
            }
        }
        return 1;
    }

    @Override
    public List<ReceiptDetail> selectReceiptDetailByIds(String[] receiptDetailIds) {
        return receiptDetailMapper.selectReceiptDetailByIds(receiptDetailIds);
    }
}
